package com.qk.controller;

import com.qk.commom.PageResult;
import com.qk.commom.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @DeleteMapping("/courses/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程id为{}",id);
        courseService.deleteById(id);
        return Result.success();


    }
    @PostMapping("/courses")
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程{}",course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/courses/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程id为{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @PutMapping("/courses")
    public Result update(@RequestBody Course course) {
        log.info("修改课程{}",course);
        courseService.update(course);
        return Result.success();
    }
    @GetMapping("/courses/list")
    public Result listAll() {
        log.info("查询所有课程");
        return Result.success(courseService.listAll());
    }
    @GetMapping("/courses/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        log.info("查询课程{}",subject);
        return Result.success(courseService.listBySubject(subject));
    }

    @GetMapping("/courses")
    public Result listCourses(@RequestParam String name,Integer subject,Integer target,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询课程{}",name,subject,target);
        PageResult< Course> pageResult = courseService.listCourses(name,subject,target,page,pageSize);

        return Result.success( pageResult);
    }



}
